package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
//@CrossOrigin
@RequestMapping("/admin/acl/role")
@Api(tags = "角色管理")
//自动帮你写一个“必须传参”的构造函数
@RequiredArgsConstructor
public class RoleController {


    private final RoleService roleService;


    /*
        url: `${api_name}/${page}/${limit}`,
        method: 'get',
        params: searchObj
        */

    @ApiOperation(value = "分页查询角色信息")
    @GetMapping("/{page}/{limit}")
    public Result getPageList(
            @ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
            @ApiParam(name = "roleQueryVo", value = "查询对象", required = false) RoleQueryVo roleQueryVo) {

        //1.创建分页对象
        Page<Role> p = new Page<>(page, limit);
        //2.创建分页条件
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(roleQueryVo.getRoleName() != null && !roleQueryVo.getRoleName().isEmpty(), Role::getRoleName, roleQueryVo.getRoleName());


        //3.执行查询
        roleService.page(p, wrapper);
        return Result.ok(p);

    }

    /*    url: `${api_name}/get/${id}`,
        method: 'get'*/
    @ApiOperation(value = "获取角色信息")
    @GetMapping("/get/{id}")
    public Result getById(
            @ApiParam(name = "id", value = "角色ID", required = true) @PathVariable String id
    ) {
        Role role = roleService.getById(id);
        return Result.ok(role);
    }


    /*  url: `${api_name}/save`,
      method: 'post',
      data: role*/
    @ApiOperation(value = "新增角色")
    @PostMapping("/save")
    public Result save(
            @ApiParam(name = "role", value = "角色信息", required = true) @RequestBody Role role
    ) {
        boolean save = roleService.save(role);
        if (!save) {
            return Result.fail("新增角色失败");
        }
        return Result.ok(null);
    }

   /* url: `${api_name}/update`,
    method: 'put',
    data: role*/

    @ApiOperation(value = "更新角色")
    @PutMapping("/update")
    public Result update(
            @ApiParam(name = "role", value = "角色信息", required = true) @RequestBody Role role
    ) {
        boolean result = roleService.updateById(role);
        if (!result) {
            return Result.fail("更新失败");
        }
        return Result.ok(null);
    }

/*
    url: `${api_name}/remove/${id}`,
    method: 'delete'*/

    @ApiOperation(value = "删除角色")
    @DeleteMapping("/remove/{id}")
    public Result remove(
            @ApiParam(name = "id", value = "角色ID", required = true) @PathVariable String id
    ) {
        boolean result = roleService.removeById(id);
        if (!result) {
            return Result.fail("删除角色失败");
        }
        return Result.ok(null);
    }

    /* url: `${api_name}/batchRemove`,
     method: 'delete',
     data: ids*/
    @ApiOperation(value = "批量删除角色")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(
            @ApiParam(name = "ids", value = "ID列表", required = true) @RequestBody List<Long> ids
    ) {
        boolean result = roleService.removeByIds(ids);
        if (!result) {
            return Result.fail("删除角色失败");
        }
        return Result.ok(null);
    }


}
